package com.chzc.fm.duotin.ui.channel;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.HotBottonAdapter;
import com.chzc.fm.duotin.adapter.recyclerview.HotTopAdapter;
import com.chzc.fm.duotin.bean.channel.ChannelType;
import com.chzc.fm.duotin.bean.channel.HotChannel;
import com.chzc.fm.duotin.utils.DeviceUtil;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.chzc.fm.duotin.widget.ObservableScrollView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/6/28.
 */
public class HotChannelActivity extends AppCompatActivity implements View.OnClickListener{

    private LinearLayout lltop,lltops;
    private ObservableScrollView scrollView;
    private TextView tvrecommend,tvupdate,tvplay,tvcrecommend,tvctitle,tvtotaltitle;
    private ImageView ivback,ivup,ivdown;
    private RecyclerView toprecycleview,bottonrecycleview;

    private HotBottonAdapter bottonAdapter;

    private String dataid;

    private HotTopAdapter topAdapter;

    private int height;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_hot_channel);

        initViews();

        initDatas();

        getHeight();

        initevents();
    }

    public void initViews()
    {
        lltop = (LinearLayout) findViewById(R.id.activity_hot_channel_top_ll );
        lltops = (LinearLayout) findViewById(R.id.activity_hot_channel_tops_ll);
        scrollView = (ObservableScrollView) findViewById(R.id.activity_hot_channel_sv);
        tvtotaltitle = (TextView) findViewById(R.id.activity_hot_channel_title_tv);

        ivback = (ImageView) findViewById(R.id.activity_hot_channel_back_iv);
        ivdown = (ImageView) findViewById(R.id.activity_hot_channel_down_iv);
        ivup = (ImageView) findViewById(R.id.activity_hot_channel_up_iv);

        tvrecommend = (TextView) findViewById(R.id.activity_hot_channel_recommend_tv);
        tvupdate = (TextView) findViewById(R.id.activity_hot_channel_update_tv);
        tvplay = (TextView) findViewById(R.id.activity_hot_channel_play_tv);
        tvcrecommend = (TextView) findViewById(R.id.activity_hot_channel_crecommed_tv);
        tvctitle = (TextView) findViewById(R.id.activity_hot_channel_ctitle_tv);

        toprecycleview = (RecyclerView) findViewById(R.id.activity_hot_channel_top_rv);
        bottonrecycleview = (RecyclerView) findViewById(R.id.activity_hot_channel_content_rv);

        GridLayoutManager topmanager = new GridLayoutManager(HotChannelActivity.this,4);
        toprecycleview.setLayoutManager(topmanager);

        GridLayoutManager bottonmanager = new GridLayoutManager(HotChannelActivity.this,2);
        bottonrecycleview.setLayoutManager(bottonmanager);

        tvrecommend.setSelected(true);

    }

    public void initDatas()
    {
        ChannelType channelType = (ChannelType) getIntent().getSerializableExtra("channeltype");
        dataid = channelType.getId();

        tvtotaltitle.setText(channelType.getTitle());

        getTopRecycleView(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {
                    LogUtil.w("开始解析数据");
                    JSONObject root = new JSONObject(object.toString());
                    String error_msg = root.getString("error_msg");

                    if("SUCCESS".equals(error_msg))
                    {
                        JSONArray data = root.getJSONArray("data");

                        List<HotChannel> dataList = HotChannel.arrayHotChannelFromData(data.toString());

                        topAdapter = new HotTopAdapter(HotChannelActivity.this,dataList);
                        toprecycleview.setAdapter(topAdapter);
                        topAdapter.setItemOnClickListener(new HotTopAdapter.mItemOnClickListener() {
                            @Override
                            public void OnItemClick(int position) {
                                switch (position)
                                {
                                    case 0:
                                        Toast.makeText(HotChannelActivity.this,"你点击的是第一个按钮",Toast.LENGTH_SHORT).show();
                                        break;
                                }
                            }
                        });

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void error(String msg) {

            }
        });


        getBotton(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {
                    LogUtil.w("开始解析数据");
                    JSONObject root = new JSONObject(object.toString());

                    String error_msg = root.getString("error_msg");
                    if("success".equals(error_msg))
                    {
                        JSONObject data = root.getJSONObject("data");
                        JSONArray datalist = data.getJSONArray("data_list");
                        List<HotChannel> hotdatalist = HotChannel.arrayHotChannelFromData(datalist.toString());

                        bottonAdapter = new HotBottonAdapter(HotChannelActivity.this,hotdatalist);
                        bottonrecycleview.setAdapter(bottonAdapter);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @TargetApi(Build.VERSION_CODES.M)
    public void initevents()
    {
        ivback.setOnClickListener(this);
        ivdown.setOnClickListener(this);
        ivup.setOnClickListener(this);
        tvrecommend.setOnClickListener(this);
        tvupdate.setOnClickListener(this);
        tvplay.setOnClickListener(this);

        scrollView.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {

                int dpy = DeviceUtil.px2dip(HotChannelActivity.this,y);
                if ((dpy >= (height - 150)) && (y-oldy) > 0)
                {
                    lltop.setVisibility(View.INVISIBLE);
                    lltops.setVisibility(View.VISIBLE);

                }else if((dpy <= height-90 ) && (y-oldy) < 0)
                {
                    lltops.setVisibility(View.GONE);
                    lltop.setVisibility(View.VISIBLE);
                }

                LogUtil.w("x = " + x + "y = " + y +  " dpy >>>>"+ DeviceUtil.px2dip(HotChannelActivity.this,y));
            }
        });



    }



    /**
     *  获取TopRecycleView的数据
     * @param callback
     */
    public void getTopRecycleView(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request  = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                LogUtil.w("GlobalParam.URL_HOTTOP_HEAD + dataid + GlobalParam.URL_HOTTOP_TAIL=====" + GlobalParam.URL_HOTTOP_HEAD + dataid + GlobalParam.URL_HOTTOP_TAIL);

                return HttpUtil.doGet(GlobalParam.URL_HOTTOP_HEAD + dataid + GlobalParam.URL_HOTTOP_TAIL);
            }
        };
        new DuotinTask(request,callback).execute();

    }

    /**
     * 获取BottonRecycleView的数据
     * @param callback
     */
    public void getBotton(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                LogUtil.w("GlobalParam.URL_HOTBOTTON_HEAD + dataid + GlobalParam.URL_HOTBOTTON_TAIL>>>" + GlobalParam.URL_HOTBOTTON_HEAD + dataid + GlobalParam.URL_HOTBOTTON_TAIL);
                return HttpUtil.doGet(GlobalParam.URL_HOTBOTTON_HEAD + dataid + GlobalParam.URL_HOTBOTTON_TAIL);
            }
        };
        new DuotinTask(request,callback).execute();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.activity_hot_channel_back_iv:
                finish();
                break;

            case R.id.activity_hot_channel_up_iv:
                lltop.setVisibility(View.GONE);
                lltops.setVisibility(View.VISIBLE);
                scrollView.smoothScrollTo(0,10);
                break;
            case R.id.activity_hot_channel_down_iv:
                lltops.setVisibility(View.GONE);
                lltop.setVisibility(View.VISIBLE);
                scrollView.smoothScrollTo(0,0);
                break;

            case R.id.activity_hot_channel_recommend_tv:
                Toast.makeText(this,"推荐",Toast.LENGTH_SHORT).show();
                tvrecommend.setSelected(true);
                tvupdate.setSelected(false);
                tvplay.setSelected(false);
                break;
            case R.id.activity_hot_channel_update_tv:
                Toast.makeText(this,"更新",Toast.LENGTH_SHORT).show();
                tvrecommend.setSelected(false);
                tvupdate.setSelected(true);
                tvplay.setSelected(false);
                break;
            case R.id.activity_hot_channel_play_tv:
                Toast.makeText(this,"播放",Toast.LENGTH_SHORT).show();
                tvrecommend.setSelected(false);
                tvupdate.setSelected(false);
                tvplay.setSelected(true);
                break;

        }
    }

    /**
     * 获取布局的高度
     */
    public void getHeight()
    {
        lltop.post(new Runnable()
        {
            @Override
            public void run() {

                lltop.measure(0,0);
                height = lltop.getMeasuredHeight();

                LogUtil.w("height >>>>>" + height);
            }
        });


    }
}
